﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using VMS.FTS.APPPUSH.Device;
using VMS.FTS.APPPUSH.Log;

namespace VMS.FTS.APPPUSH.Plugins
{
    public class PluginsManager :ILogable
    {
        #region | Singleton construction |
        private static readonly Lazy<PluginsManager> lazy =
            new Lazy<PluginsManager>(() => new PluginsManager());

        public static PluginsManager Instance { get { return lazy.Value; } }
        #endregion

        #region | Constructors |

        string pluginsPath;

        List<Type> DeviceTypeList;
        public PluginsManager()
        {

            LOG_TAG = this.GetType().Name;
            LOG(MethodBase.GetCurrentMethod().Name, "Init plugins system", LOG_LEVEL.INFO);

            pluginsPath = Properties.Settings.Default.WORKING_DIRECTORY +
                Properties.Settings.Default.PLUGINS_DIRECTORY;
            DeviceTypeList = new List<Type>();
            // load all dll file
            foreach (string path in Directory.GetFiles(pluginsPath, "*.dll"))
            {
                Assembly deviceAsm = Assembly.LoadFrom(path);
                if (deviceAsm == null) continue;
                foreach (Type devType in deviceAsm.GetTypes())
                {
                    if (devType.BaseType == typeof(VMSDevice))
                    {
                        LOG(MethodBase.GetCurrentMethod().Name, "Found device type: " + devType.Name, LOG_LEVEL.INFO);
                        DeviceTypeList.Add(devType);
                    }
                }
                
            }
            
        }

        public List<Type> GetSupportedDeviceType()
        {
            return DeviceTypeList;
        }


        #endregion

    }
}
